package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.example.myapplication.Bean.PyqItem;

import java.util.Date;

/*发朋友圈*/
public class PostActivity extends AppCompatActivity {
    private EditText pyqedit;
    private RadioGroup chooseimg,chooseimg1;
    private Button pyqbtn;
    //    定义一个变量
    int contentimg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        initView();
        setListener();
    }

    private void setListener() {
        /*选择图片控件*/
        chooseimg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radiobtn1:
                        contentimg = R.mipmap.zuozub;
                        break;
                    case R.id.radiobtn2:
                        contentimg = R.mipmap.yuanleimu;
                        break;
                    case R.id.radiobtn3:
                        contentimg = R.mipmap.guidao03;
                        break;
//                    case R.id.radiobtn4:
//                        contentimg = R.mipmap.syml;
//                        break;
//                    case R.id.radiobtn5:
//                        contentimg = R.mipmap.ailian;
//                        break;
//                    case R.id.radiobtn6:
//                        contentimg = R.mipmap.sanli;
//                        break;
                }

            }

        });


        /*  发表按钮*/
        pyqbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (contentimg != 0) {//非空判断
                    SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
                    PyqItem p = new PyqItem();  //无参构造方法
                    p.setId(0);
                    p.setUserimg(sp.getInt("myimg", -1));
                    p.setUsername(sp.getString("myname", ""));
//                    输入框中的文字
                    p.setPyqcontent(pyqedit.getText().toString());
//                    发表的图片
                    p.setPyqimg(contentimg);
                    p.setDate(new Date());
                    if (p.save()) {
                        Intent intent = new Intent(PostActivity.this, PyqActivity.class);
                        startActivity(intent);
                        finish();

                    }
                }

            }


        });

    }

    private void initView() {

        pyqedit = findViewById(R.id.pyqedit);
        chooseimg = findViewById(R.id.chooseimg);
        pyqbtn = findViewById(R.id.pyqbtn);


    }
}
